package com.enzo.prms.common.base.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;

import com.enzo.prms.common.base.dao.BaseDAO;
import com.enzo.prms.common.base.to.BaseEntity;
import com.enzo.prms.common.exception.base.DAOException;
import com.enzo.prms.common.exception.base.ServiceException;


public class BaseServiceImpl<T extends BaseEntity> implements BaseService<T> {
    private BaseDAO<T> baseDAO;

    public void save(T obj)throws ServiceException{
        try {
            baseDAO.save(obj);
        } catch (Exception e){
            throw new ServiceException("Save Error!",e);
        }
    }

    public void delete(T obj){
        baseDAO.delete(obj);

    }
    public void delete(Integer id){
        baseDAO.delete(id);
    }

	public void remove(Integer id) throws ServiceException {
		try {
			T obj = baseDAO.getById(id);
			if (obj != null) {
				((BaseEntity) obj).setFlag(0);
				baseDAO.update(obj);
			}
		} catch (DAOException e) {
			throw new ServiceException(e);
		}
	}

    public void update(T obj)throws ServiceException{
        try {
			baseDAO.update(obj);
		} catch (DAOException e) {
			throw new ServiceException(e);
		}
    }

    public T getById(Integer id){
        return baseDAO.getById(id);
    }

    public T load(Integer id){
        return baseDAO.load(id);
    }
    
	public void executeByHql(String hql, Map<String, Object> params)
			throws ServiceException {
		try {
			baseDAO.executeByHql(hql, params);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}
	
	public List<T> getListByCriteria(DetachedCriteria criteria) throws ServiceException {
		try {
			return baseDAO.getListByCriteria(criteria);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	public List<T> getListByHQL(String hql,Map<String, Object> params){
    	return baseDAO.getListByHQL(hql, params);
    }
   
	public List<T> getListBySQL(String sql, Map<String, Object> params) {
		return baseDAO.getListBySQL(sql, params);
	}

	public List<T> getListByNamedSQL(String sqlName, Map<String, Object> params) {
		return baseDAO.getListByNamedSQL(sqlName, params);
	}

	public long count(String sql) {
		return baseDAO.count(sql);
	}

	public List<T> getListForPage(String sql, Map<String, Object> params, int offset,
			int length) {
		return baseDAO.getListForPage(sql, params, offset, length);
	}

	public BaseDAO<T> getBaseDAO() {
		return baseDAO;
	}

	@Resource
	public void setBaseDAO(BaseDAO<T> baseDAO) {
		this.baseDAO = baseDAO;
	}
    
}